<template>
  <div class="expenditure-summary">
    <menu-content-layout title="支出概要">
      <template v-slot:icon>
        <img src="~@/assets/images/expenditure-summary.png" alt="" />
      </template>
      <template v-slot:content>
        <div class="history">
          <div class="title">
            历史采购额
            <div class="brackets">(过去36个月)</div>
          </div>
        </div>
        <div class="chart-wrap">
          <div class="title">{{ lineItem[35] }} - {{ lineItem[0] }}</div>
          <div id="line-chart" class="line-chart"></div>
        </div>
        <div class="ranking">
          <div class="item supplier">
            <div class="title">
              TOP10供应商（过去12个月）
              <router-link class="button" :to="{ name: 'MySupplier' }">
                查看所有
                <img src="~/assets/images/right-arrow-white.png" alt="" />
              </router-link>
            </div>
            <div class="list">
              <div class="list-item" v-for="(item, index) in shopsList" :key="index">
                <div class="index">{{ index + 1 }}.</div>
                <div class="name">{{item.mer_name}}</div>
                <div class="value">{{item.total_price}}元</div>
              </div>
            </div>
          </div>
          <div class="item product">
            <div class="title">
              TOP10品类（过去12个月）
              <router-link class="button" :to="{ name: 'MySupplier' }">
                查看所有
                <img src="~/assets/images/right-arrow-white.png" alt="" />
              </router-link>
            </div>
            <div class="list">
              <div class="list-item" v-for="(item, index) in productsList" :key="index">
                <div class="index">{{ index + 1 }}.</div>
                <div class="name">{{ item.store_name }}</div>
                <div class="value">{{item.product_price}}元</div>
              </div>
            </div>
          </div>
        </div>
      </template>
    </menu-content-layout>
  </div>
</template>

<script>
import MenuContentLayout from "@/components/common/menu-content-layout/MenuContentLayout"
import { getCountLog, getMerchantLog, getProductLog } from "@/request/api"

export default {
  name: "ExpenditureSummary",
  components: { MenuContentLayout },
  data() {
    return {
      lineItem: [],
      optionData: [],
      shopsList: [], // top商铺
      productsList: []
    }
  },
  mounted() {
    this.getLog()
    this.getLastLog()
  },
  methods: {
    // 获取TOP10供应商（过去12个月）
    getLastLog() {
      getMerchantLog().then((res) => {
        if (res.status === 200) {
          this.shopsList = res.data
        }
      })
      getProductLog().then((res) => {
        if (res.status === 200) {
          this.productsList = res.data
        }
      })
    },
    // 获取我的采购
    getLog() {
      getCountLog().then((res) => {
        if (res.status === 200) {
          let lineItem = []
          let optionData = []
          res.data.map((item) => {
            lineItem.push(`${item.year}年${item.month}月`)
            optionData.push(item.count)
          })
          this.lineItem = lineItem
          this.optionData = optionData
          this.initLineChart()
        }
      })
    },
    initLineChart() {
      const echarts = this.$echarts.init(document.getElementById("line-chart"))
      const options = {
        xAxis: {
          type: "category",
          data: this.lineItem
        },
        yAxis: {
          type: "value"
        },
        tooltip: {
          trigger: "axis",
          formatter: function (params) {
            params = params[0].data
            return params + "元"
          },
          axisPointer: {
            animation: false
          }
        },
        series: [
          {
            data: this.optionData,
            type: "line"
          }
        ],
        lineStyle: {
          color: "#E48B2C"
        }
      };
      echarts.setOption(options)
    }
  }
}
</script>

<style lang="scss" scoped>
@import "~@/assets/style/common.scss";

.expenditure-summary {
  .history {
    font-weight: bold;
    font-size: 18px;
    display: flex;
    align-items: center;
    margin-bottom: 15px;

    &:after {
      content: "";
      display: inline-block;
      width: 76%;
      border-bottom: solid 1px #eaeaea;
    }

    .title {
      display: flex;
      align-items: center;

      .brackets {
        font-weight: normal;
        font-size: 14px;
        margin-left: 10px;
      }
    }
  }

  .chart-wrap {
    margin-bottom: 15px;

    .title {
      color: $theme-color-1;
      font-weight: bold;
      margin-bottom: 15px;
      display: flex;
      justify-content: center;
    }

    .line-chart {
      height: 260px;
    }
  }

  .ranking {
    display: flex;
    justify-content: space-between;

    .item {
      width: 400px;

      .title {
        height: 50px;
        background-color: $theme-color-1;
        color: #fff;
        font-size: 16px;
        font-weight: bold;
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding-left: 15px;

        img {
          margin-left: 10px;
          margin-right: 15px;
        }
      }

      .list {
        border-right: solid 1px #e2e2e2;
        border-left: solid 1px #e2e2e2;
        border-bottom: solid 1px #e2e2e2;

        .list-item {
          padding: 12px 20px;
          display: flex;
          position: relative;
          font-size: 15px;
          color: #666;

          .name {
            position: absolute;
            left: 65px;
          }

          .value {
            position: absolute;
            right: 0;
            margin-right: 20px;
          }

          &:nth-child(odd) {
            background-color: #fff;
          }

          &:nth-child(even) {
            background-color: #f3f4f7;
          }

          &:hover {
            color: $theme-color-1;
          }
        }
      }
    }
  }
}
</style>
